package com.bkitmobile.kesu.appengine.storingdata.server.entity;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.beoui.geocell.GeocellManager;
import com.beoui.geocell.model.LocationCapable;
import com.beoui.geocell.model.Point;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;


import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Place implements LocationCapable {
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	@Persistent
	private Key userCreate;
	
	@Persistent
	private Text info;
	
	@Persistent
	private Text address;
	
	@Persistent
	private String imageLocationURL;
	
	@Persistent
	private double longtitude;
	
	@Persistent
	private double latitude;
	
	@Persistent
	private String name;
	
	@Persistent
	private String type;
	
	@Persistent
    private List<String> geocells;

	@Persistent
    private boolean isPublic;
    
	
	@Persistent(serialized = "true")
	private List<CommentsLocation> listCmt;
	
	public static final int MAX_PLACE = 50;
	public static final int MAX_PLACE_ALERT = 5;
	public static final double DISTANCE_LOAD_MAP = 2000.0;
	public static final double DISTANCE_ALERT = 250.0;
	public Place(Key userCreate,String info,String imageURL,double longtitude,
			double latitude,String type) 
	{
		key = KeyFactory.createKey(Place.class.getSimpleName(), new String(longtitude+""+latitude));
		this.info = new Text(info);
		this.imageLocationURL = imageURL;
		this.longtitude = longtitude;
		this.latitude = latitude;
		this.userCreate = userCreate;
		this.geocells = GeocellManager.generateGeoCell(new Point(latitude, longtitude));
		this.setType(type);
		setPublic(true);
	}
	public Place(Key userCreate,String info,double longtitude,
			double latitude,String name,String addr,String type) 
	{
		key = KeyFactory.createKey(Place.class.getSimpleName(), new String((int)(longtitude*1E6)+""+(int)(latitude*1E6)));
		this.info = new Text(info);
		this.longtitude = longtitude;
		this.latitude = latitude;
		this.userCreate = userCreate;
		this.geocells = GeocellManager.generateGeoCell(new Point(latitude, longtitude));
		this.setName(name);
		this.address = new Text(addr);
		this.setType(type);
		setPublic(true);
	}
	public void setKey(Key key) {
		this.key = key;
	}

	public Key getKey() {
		return key;
	}

	public void setUserCreate(Key userCreate) {
		this.userCreate = userCreate;
	}

	public Key getUserCreate() {
		return userCreate;
	}


	public void setInfo(String info) {
		this.info = new Text(info);
	}

	public String getInfo() {
		return info.getValue();
	}

	public void setAddress(String address) {
		address = address;
	}

	public String getAddress() {
		return address.getValue();
	}

	public void setImageLocationURL(String imageLocationURL) {
		this.imageLocationURL = imageLocationURL;
	}

	public String getImageLocationURL() {
		return imageLocationURL;
	}

	public void setLongtitude(double longtitude) {
		this.longtitude = longtitude;
	}

	public double getLongtitude() {
		return longtitude;
	}

	public void setLattitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLattitude() {
		return latitude;
	}

	public void setGeocells(List<String> geocells) {
		this.geocells = geocells;
	}

	public List<String> getGeocells() {
		return geocells;
	}

	@Override
	public String getKeyString() {
		// TODO Auto-generated method stub
		return key.toString();
	}

	@Override
	public Point getLocation() {
		// TODO Auto-generated method stub
		return new Point(latitude,longtitude);
	}

	public void setListCmt(List<CommentsLocation> listCmt) {
		this.listCmt = listCmt;
	}

	public List<CommentsLocation> getListCmt() {
		return listCmt;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getType() {
		return type;
	}
	public void setPublic(boolean isPublic) {
		this.isPublic = isPublic;
	}
	public boolean isPublic() {
		return isPublic;
	}
	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof Place)
		{
			return key.equals(((Place) obj).getKey());
		}
		else return false;
	}
	

}
